﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Objects;
using System.Linq;

namespace Project5188.BUS.EducationAndTraining
{
    public class TeacherRegistries : DetailView<teacherregistry>
    {
        private project5188Entities _context;
        // ReSharper disable InconsistentNaming
        private new List<teacherregistry> EntityList;
        private new teacherregistry Entity;
        // ReSharper restore InconsistentNaming

        public long TransactionId { get; private set; }

        #region Overrides of DetailView<teacherregistry>

        public override List<teacherregistry> GetEntityList()
        {
            _context = new project5188Entities();
            EntityList = _context.teacherregistries.Where(a => !a.IsDeleted).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public override List<teacherregistry> GetEntityById(long id)
        {
            _context = new project5188Entities();
            EntityList = _context.teacherregistries.Where(a => a.a_ID == id && !a.IsDeleted).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public override teacherregistry GetEntity()
        {
            _context = new project5188Entities();
            Entity = new teacherregistry();
            return Entity;
        }

        public override void Update()
        {
            _context.SaveChanges();
        }

        public override void Insert()
        {
            _context.AddToteacherregistries(Entity);
            _context.SaveChanges();
            TransactionId = Entity.a_ID;
        }

        #endregion

        public List<GetTeacherRegistryInfoListByParameters_Result> GetEntityListByParameters(string registryNoText, string nameSurname, string tcNoText, string courseName, string coursePeriod)
        {
            _context = new project5188Entities();

            int tcNo = 0, registryNo = 0;

            if (!String.IsNullOrEmpty(tcNoText) && !String.IsNullOrWhiteSpace(tcNoText))
            {
                Int32.TryParse(tcNoText, out tcNo);
            }

            if (!String.IsNullOrEmpty(registryNoText) && !String.IsNullOrWhiteSpace(registryNoText))
            {
                Int32.TryParse(registryNoText, out registryNo);
            }

            var objectParameters = new ObjectParameter[5];

            var registryParameter = new ObjectParameter("RegistryNo", SqlDbType.Int) { Value = registryNo };
            objectParameters[0] = registryParameter;

            var nameSurnameParameter = new ObjectParameter("NameSurname", SqlDbType.VarChar) { Value = nameSurname };
            objectParameters[1] = nameSurnameParameter;

            var tcNoParameter = new ObjectParameter("TCNo", SqlDbType.Int) { Value = tcNo };
            objectParameters[2] = tcNoParameter;

            var courseNameParameter = new ObjectParameter("CourseName", SqlDbType.VarChar) { Value = courseName };
            objectParameters[3] = courseNameParameter;

            var coursePeriodParameter = new ObjectParameter("CoursePeriod", SqlDbType.VarChar) { Value = coursePeriod };
            objectParameters[4] = coursePeriodParameter;

            var list = _context.ExecuteFunction<GetTeacherRegistryInfoListByParameters_Result>("GetTeacherRegistryInfoListByParameters",
                                                                  objectParameters).ToList();

            return list;
        }
    }
}
